package com.parenting.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.parenting.entities.ShopCollectionDO;
import com.parenting.mapper.ShopCollectionMapper;
import com.parenting.model.IMPage;
import com.parenting.request.CollectionSearchRequest;
import com.parenting.response.CollectionListResponse;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.parenting.service.ShopCollectionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 店铺收藏表  服务实现类
 * </p>
 *
 * @author 烟台业达-王世玺
 * @since 2021-09-26
 */
@Service
@Slf4j
public class ShopCollectionServiceImpl extends ServiceImpl<ShopCollectionMapper, ShopCollectionDO> implements ShopCollectionService {


    @Override
    public IMPage<CollectionListResponse> getCollection(CollectionSearchRequest request,String shopId) {
        LambdaQueryWrapper<ShopCollectionDO> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(ShopCollectionDO::getShopId,shopId);
        IPage<ShopCollectionDO> shopCollectionDOIPage = baseMapper.selectPage(request.getPage(), wrapper);
        IMPage<CollectionListResponse>  imPage=new IMPage<>();
        imPage.setTotal(shopCollectionDOIPage.getTotal());
        imPage.setCurrent(shopCollectionDOIPage.getCurrent());
        imPage.setSize(shopCollectionDOIPage.getSize());
        List<CollectionListResponse> list=new ArrayList<>();
        if (shopCollectionDOIPage.getTotal()>0){
            for (ShopCollectionDO s:shopCollectionDOIPage.getRecords()) {
                list.add(BeanUtil.copyProperties(s,CollectionListResponse.class));
            }
            imPage.setRecords(list);
        }else{
            imPage.setRecords(new ArrayList<>());
        }
        return imPage;
    }
}
